#include "chesspiece.h"
#include <QPainter>
#include <QDebug>

ChessPiece::ChessPiece(double sx, double sy, double r,int id) : QGraphicsEllipseItem(sx,sy,r,r)
{
    this->sx=sx;
    this->sy=sy;
    this->r=r;
    this->id=id;
}

ChessPiece::~ChessPiece()
{

}

void ChessPiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->save();
    if(selected){
        painter->setBrush(QColor(51, 248, 255));
    }else{
        painter->setBrush(QColor(155,125,10));
    }
    double nx=sx+cx*2*r;
    double ny=sy+cy*2*r;
    painter->drawEllipse(QPointF(nx,ny),r,r);
    QRectF rect(nx-0.8*r,ny-0.8*r,1.6*r,1.6*r);
    painter->setFont(QFont("隶书",r,QFont::Bold));
    QPen pen;
    pen.setStyle(Qt::SolidLine);
    if(side==1){
        pen.setColor(Qt::red);
    }else{
        pen.setColor(Qt::black);
    }
    pen.setWidth(2);
    painter->setPen(pen);
    painter->drawText(rect,textName,QTextOption(Qt::AlignCenter));
    painter->restore();
}

void ChessPiece::initChessPiece(const QString testName, double cx, double cy, int side,int kind)
{
    this->textName=testName;
    this->cx=cx;
    this->cy=cy;
    this->side=side;
    this->kind=kind;
}

void ChessPiece::newPos(double cx, double cy)
{
    this->cx=cx;
    this->cy=cy;
    this->setSelected(false);
    //update();
}

void ChessPiece::setSelected(bool b)
{
    this->selected=b;
    //update();
}

int ChessPiece::getId() const
{
    return id;
}

double ChessPiece::getCx() const
{
    return cx;
}

double ChessPiece::getCy() const
{
    return cy;
}

QString ChessPiece::getTextName() const
{
    return textName;
}

int ChessPiece::getSide() const
{
    return side;
}

int ChessPiece::getKind() const
{
    return kind;
}


//点击事件处理
void ChessPiece::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

    //this->cx+1;
    QGraphicsEllipseItem::mousePressEvent(event);
    qDebug()<<"cx="<<cx<<"cy="<<cy;
    //update();
}
